<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>登录页面</title>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            animation: gradient 15s ease infinite;
            background-size: 400% 400%;
        }

        @keyframes gradient {
            0% {
                background-position: 0% 50%;
            }

            50% {
                background-position: 100% 50%;
            }

            100% {
                background-position: 0% 50%;
            }
        }

        /* 登录表单容器 */
        .login-container {
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            width: 90%;
            max-width: 400px;
            transition: transform 0.3s ease;
        }

        .login-container:hover {
            transform: translateY(-5px);
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
            font-size: 24px;
        }

        /* 输入框样式 */
        .input-group {
            margin-bottom: 18px;
            position: relative;
        }

        .input-group input {
            width: 100%;
            padding: 13px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }

        .input-group input:focus {
            outline: none;
            border-color: #667eea;
        }

        .input-group label {
            position: absolute;
            left: 13px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
            pointer-events: none;
            transition: all 0.3s ease;
            font-size: 14px;
        }

        .input-group input:focus+label,
        .input-group input:valid+label {
            top: 0;
            font-size: 11px;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 0 5px;
            color: #667eea;
        }

        /* 登录按钮样式 */
        button {
            width: 100%;
            padding: 13px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            transition: opacity 0.3s ease;
        }

        button:hover {
            opacity: 0.9;
        }

        /* 消息提示样式 */
        #message {
            margin-top: 18px;
            text-align: center;
            padding: 8px;
            border-radius: 8px;
            font-size: 12px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }

        /* 手机端适配 */
        @media (max-width: 480px) {
            .login-container {
                padding: 25px;
            }

            h2 {
                font-size: 22px;
                margin-bottom: 20px;
            }

            .input-group input {
                padding: 12px;
                font-size: 13px;
            }

            .input-group label {
                left: 12px;
                font-size: 13px;
            }

            .input-group input:focus+label,
            .input-group input:valid+label {
                font-size: 10px;
            }

            button {
                padding: 12px;
                font-size: 13px;
            }

            #message {
                margin-top: 15px;
                font-size: 11px;
            }
        }
    </style>
</head>

<body>
<div class="login-container">
    <h2>欢迎登录</h2>
    <form id="loginForm">
        <div class="input-group">
            <input type="text" id="username" placeholder=" " required>
            <label for="username">用户名</label>
        </div>
        <div class="input-group">
            <input type="password" id="password" placeholder=" " required>
            <label for="password">密码</label>
        </div>
        <button type="submit">登录</button>
        <div id="message"></div>
    </form>
</div>

<script>
    // 定义一个函数用于设置 cookie
    function setCookie(name, value, days) {
        let expires = "";
        if (days) {
            const date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "") + expires + "; path=/";
    }

    const loginForm = document.getElementById('loginForm');
    const messageDiv = document.getElementById('message');

    loginForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
        })
            .then(response => response.text())
            .then(data => {
                if (data.includes('用户 ID:')) {
                    // 提取用户 ID
                    const startIndex = data.indexOf('用户 ID: ') + 7;
                    const userId = data.slice(startIndex).trim();
                    // 将用户 ID 存储到 cookie 中，设置有效期为 1 天
                    setCookie('userId', userId, 1);
                    // 跳转到 index 页面
                    window.location.href = 'index';
                } else {
                    messageDiv.textContent = data;
                    messageDiv.className = 'error';
                }
            })
            .catch(error => {
                messageDiv.textContent = '请求出错，请重试';
                messageDiv.className = 'error';
            });
    });
</script>
</body>

</html>